﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Threading;
using S7.Net;
using S7.Net.Types;
//using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar;
using Button = System.Windows.Forms.Button;

namespace thinger.VisionDemo
{
    public partial class Frm水洗装置 : Form
    {
        Plc siemens;
        private System.Threading.Timer _timer;

        bool 清扫电机1Q;
        bool 电机50UQ;
        bool 电磁阀11Q;
        bool 电磁阀12Q;
        bool 电磁阀13Q;
        bool 电磁阀21Q;
        bool 三轴缸1上Q;
        bool 三轴缸1下Q;
        bool 三轴缸2上Q;
        bool 三轴缸2下Q;
        bool 三轴缸3上Q;
        bool 三轴缸3下Q;
        bool 三轴缸4上Q;
        bool 三轴缸4下Q;
        bool 气振1Q;
        bool 气振2Q;
        bool 气振3Q;
        bool 气振4Q;
        bool 气振5Q;
        bool 气振6Q;
        bool 气振7Q;
        bool 气振8Q;
        bool 无杆杠上Q;
        bool 无杆缸下Q;
        bool 电机振动1Q;
        bool 电机振动2Q;
        bool 电机振动3Q;
        bool 电机振动4Q;
        float 液位;

        public Frm水洗装置()
        {
            InitializeComponent();
            // 创建计时器，但不立即启动
            _timer = new System.Threading.Timer(TimerCallback, null, Timeout.Infinite, Timeout.Infinite);
        }

        private void TimerCallback(object state)
        {
            // 这里模拟获取数据（在后台线程）
            //string timeString = System.DateTime.Now.ToString("HH:mm:ss.fff");
                //txt_料桶升降伺服速度.Text = timeString;


                //读取料桶升降伺服使能状态
                short 料桶升降伺服使能状态 = (short)siemens.Read(DataType.DataBlock, 4, 2, VarType.Int, 1);
                //Console.WriteLine($"读取值: {料桶升降伺服使能状态}");
                //读取桶盖伺服使能状态
                short 桶盖伺服使能状态 = (short)siemens.Read(DataType.DataBlock, 4, 36, VarType.Int, 1);
                //Console.WriteLine($"读取值: {桶盖伺服使能状态}");
                //读取桶旋转伺服使能状态
                short 桶旋转伺服使能状态 = (short)siemens.Read(DataType.DataBlock, 4, 70, VarType.Int, 1);
                //Console.WriteLine($"读取值: {桶旋转伺服使能状态}");
                //读取桶翻转伺服使能状态
                short 桶翻转伺服使能状态 = (short)siemens.Read(DataType.DataBlock, 4, 104, VarType.Int, 1);
                //Console.WriteLine($"读取值: {桶翻转伺服使能状态}");
                 //读取清理翻转伺服使能状态
                short 清理翻转伺服使能状态 = (short)siemens.Read(DataType.DataBlock, 4, 138, VarType.Int, 1);
                //Console.WriteLine($"读取值: {清理翻转伺服使能状态}");
                //读取清理升降伺服使能状态
                short 清理升降伺服使能状态 = (short)siemens.Read(DataType.DataBlock, 4, 172, VarType.Int, 1);
                //Console.WriteLine($"读取值: {清理升降伺服使能状态}");
                //读取下料摇筛伺服使能状态
                short 下料摇筛伺服使能状态 = (short)siemens.Read(DataType.DataBlock, 4, 210, VarType.Int, 1);
                //Console.WriteLine($"读取值: {下料摇筛伺服使能状态}");


                //读取料桶升降伺服实际位置
                float 料桶升降伺服实际位置 = (float)siemens.Read(DataType.DataBlock, 4, 24, VarType.Real, 1);
                //Console.WriteLine($"读取值: {料桶升降伺服实际位置}");
                 //读取桶盖伺服实际位置
                float 桶盖伺服实际位置 = (float)siemens.Read(DataType.DataBlock, 4, 62, VarType.Real, 1);
                //Console.WriteLine($"读取值: {桶盖伺服实际位置}");
                //读取桶旋转伺服实际位置
                float 桶旋转伺服实际位置 = (float)siemens.Read(DataType.DataBlock, 4, 96, VarType.Real, 1);
                //Console.WriteLine($"读取值: {桶旋转伺服实际位置}");
                //读取桶翻转伺服实际位置
                float 桶翻转伺服实际位置 = (float)siemens.Read(DataType.DataBlock, 4, 126, VarType.Real, 1);
                //Console.WriteLine($"读取值: {桶翻转伺服实际位置}");
                //读取清理翻转伺服实际位置
                float 清理翻转伺服实际位置 = (float)siemens.Read(DataType.DataBlock, 4, 160, VarType.Real, 1);
                //Console.WriteLine($"读取值: {清理翻转伺服实际位置}");
                //读取清理升降伺服实际位置
                float 清理升降伺服实际位置 = (float)siemens.Read(DataType.DataBlock, 4, 194, VarType.Real, 1);
                //Console.WriteLine($"读取值: {清理升降伺服实际位置}");
                //读取下料摇筛伺服实际位置
                float 下料摇筛伺服实际位置 = (float)siemens.Read(DataType.DataBlock, 4, 218, VarType.Real, 1);
                //Console.WriteLine($"读取值: {下料摇筛伺服实际位置}");


                //读取料桶升降伺服状态
                bool 料桶升降伺服状态 = (bool)siemens.Read(DataType.DataBlock, 4, 32, VarType.Bit, 1, 0);
                //Console.WriteLine($"读取值: {料桶升降伺服状态}");
                //读取桶盖伺服状态
                bool 桶盖伺服状态 = (bool)siemens.Read(DataType.DataBlock, 4, 66, VarType.Bit, 1, 0);
                //Console.WriteLine($"读取值: {桶盖伺服状态}");
                //读取桶旋转伺服状态
                bool 桶旋转伺服状态 = (bool)siemens.Read(DataType.DataBlock, 4, 100, VarType.Bit, 1, 0);
                //Console.WriteLine($"读取值: {桶旋转伺服状态}");
                //读取桶翻转伺服状态
                bool 桶翻转伺服状态 = (bool)siemens.Read(DataType.DataBlock, 4, 134, VarType.Bit, 1, 0);
                //Console.WriteLine($"读取值: {桶翻转伺服状态}");
                //读取清理翻转伺服状态
                bool 清理翻转伺服状态 = (bool)siemens.Read(DataType.DataBlock, 4, 168, VarType.Bit, 1, 0);
                //Console.WriteLine($"读取值: {清理翻转伺服状态}");
                //读取清理升降伺服状态
                bool 清理升降伺服状态 = (bool)siemens.Read(DataType.DataBlock, 4, 202, VarType.Bit, 1, 0);
                //Console.WriteLine($"读取值: {清理升降伺服状态}");
                //读取下料摇筛伺服状态
                bool 下料摇筛伺服状态 = (bool)siemens.Read(DataType.DataBlock, 4, 240, VarType.Bit, 1, 0);
                //Console.WriteLine($"读取值: {下料摇筛伺服状态}");


            //读取状态
            清扫电机1Q = (bool)siemens.Read("Q0.5");
            电机50UQ = (bool)siemens.Read("Q1.1");
            电磁阀11Q = (bool)siemens.Read("Q8.0");
            电磁阀12Q = (bool)siemens.Read("Q8.1");
            电磁阀13Q = (bool)siemens.Read("Q8.2");
            电磁阀21Q = (bool)siemens.Read("Q8.3");
            三轴缸1上Q = (bool)siemens.Read("Q8.6");
            三轴缸1下Q = (bool)siemens.Read("Q8.7");
            三轴缸2上Q = (bool)siemens.Read("Q9.0");
            三轴缸2下Q = (bool)siemens.Read("Q9.1");
            三轴缸3上Q = (bool)siemens.Read("Q9.2");
            三轴缸3下Q = (bool)siemens.Read("Q9.3");
            三轴缸4上Q = (bool)siemens.Read("Q9.4");
            三轴缸4下Q = (bool)siemens.Read("Q9.5");
            气振1Q = (bool)siemens.Read("Q9.6");
            气振2Q = (bool)siemens.Read("Q9.7");
            气振3Q = (bool)siemens.Read("Q12.0");
            气振4Q = (bool)siemens.Read("Q12.1");
            气振5Q = (bool)siemens.Read("Q12.2");
            气振6Q = (bool)siemens.Read("Q12.3");
            气振7Q = (bool)siemens.Read("Q12.4");
            气振8Q = (bool)siemens.Read("Q12.5");
            无杆杠上Q = (bool)siemens.Read("Q12.6");
            无杆缸下Q = (bool)siemens.Read("Q12.7");
            电机振动1Q = (bool)siemens.Read("Q13.2");
            电机振动2Q = (bool)siemens.Read("Q13.3");
            电机振动3Q = (bool)siemens.Read("Q13.4");
            电机振动4Q = (bool)siemens.Read("Q13.5");
            // 读取 MD74 的 Real 值
            // 地址格式："MD74" 表示内存区双字，起始字节为74
            //var resultMD74 = siemens.Read("MD74");

            //// 转换为 float 类型（Real 对应 C# 中的 float）
            //if (resultMD74 is float realValue)
            //{
            //    液位 = realValue;
            //}
            //else
            //{
            //    液位 = 0f;
            //}


            液位 = BitConverter.ToSingle(ReadAsBytes(siemens, "MD74", 4), 0);// Real类型占4字节
            



            // 使用BeginInvoke将更新操作封送到UI线程
            BeginInvoke((Action)(() =>
            {


                #region 使能状态
                if (料桶升降伺服使能状态 == (short)1)
                {
                    if (btn_料桶升降伺服使能.BackColor != Color.Red)
                        btn_料桶升降伺服使能.BackColor = Color.Red;
                }
                else
                {
                    if (btn_料桶升降伺服使能.BackColor != Color.FromArgb(0, 0, 64))
                        btn_料桶升降伺服使能.BackColor = Color.FromArgb(0, 0, 64);
                }
                if (桶盖伺服使能状态 == (short)1)
                {
                    if (btn_桶盖伺服使能.BackColor != Color.Red)
                        btn_桶盖伺服使能.BackColor = Color.Red;
                }
                else
                {
                    if (btn_桶盖伺服使能.BackColor != Color.FromArgb(0, 0, 64))
                        btn_桶盖伺服使能.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (桶旋转伺服使能状态 == (short)1)
                {
                    if (btn_桶旋转伺服使能.BackColor != Color.Red)
                        btn_桶旋转伺服使能.BackColor = Color.Red;
                }
                else
                {
                    if (btn_桶旋转伺服使能.BackColor != Color.FromArgb(0, 0, 64))
                        btn_桶旋转伺服使能.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (桶翻转伺服使能状态 == (short)1)
                {
                    if (btn_桶翻转伺服使能.BackColor != Color.Red)
                        btn_桶翻转伺服使能.BackColor = Color.Red;
                }
                else
                {
                    if (btn_桶翻转伺服使能.BackColor != Color.FromArgb(0, 0, 64))
                        btn_桶翻转伺服使能.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (清理翻转伺服使能状态 == (short)1)
                {
                    if (btn_清理翻转伺服使能.BackColor != Color.Red)
                        btn_清理翻转伺服使能.BackColor = Color.Red;
                }
                else
                {
                    if (btn_清理翻转伺服使能.BackColor != Color.FromArgb(0, 0, 64))
                        btn_清理翻转伺服使能.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (清理升降伺服使能状态 == (short)1)
                {
                    if (btn_清理升降伺服使能.BackColor != Color.Red)
                        btn_清理升降伺服使能.BackColor = Color.Red;
                }
                else
                {
                    if (btn_清理升降伺服使能.BackColor != Color.FromArgb(0, 0, 64))
                        btn_清理升降伺服使能.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (下料摇筛伺服使能状态 == (short)1)
                {
                    if (btn_下料摇筛伺服使能.BackColor != Color.Red)
                        btn_下料摇筛伺服使能.BackColor = Color.Red;
                }
                else
                {
                    if (btn_下料摇筛伺服使能.BackColor != Color.FromArgb(0, 0, 64))
                        btn_下料摇筛伺服使能.BackColor = Color.FromArgb(0, 0, 64);
                }

                #endregion
                #region 实际位置
                if (txt_料桶升降伺服实际位置.Text != 料桶升降伺服实际位置.ToString())
                    txt_料桶升降伺服实际位置.Text = 料桶升降伺服实际位置.ToString();

                if (txt_桶盖伺服实际位置.Text != 桶盖伺服实际位置.ToString())
                    txt_桶盖伺服实际位置.Text = 桶盖伺服实际位置.ToString();

                if (txt_桶旋转伺服实际位置.Text != 桶旋转伺服实际位置.ToString())
                    txt_桶旋转伺服实际位置.Text = 桶旋转伺服实际位置.ToString();

                if (txt_桶翻转伺服实际位置.Text != 桶翻转伺服实际位置.ToString())
                    txt_桶翻转伺服实际位置.Text = 桶翻转伺服实际位置.ToString();

                if (txt_清理翻转伺服实际位置.Text != 清理翻转伺服实际位置.ToString())
                    txt_清理翻转伺服实际位置.Text = 清理翻转伺服实际位置.ToString();

                if (txt_清理升降伺服实际位置.Text != 清理升降伺服实际位置.ToString())
                    txt_清理升降伺服实际位置.Text = 清理升降伺服实际位置.ToString();

                if (txt_下料摇筛伺服实际位置.Text != 下料摇筛伺服实际位置.ToString())
                    txt_下料摇筛伺服实际位置.Text = 下料摇筛伺服实际位置.ToString();

                #endregion
                #region 伺服状态
                if (料桶升降伺服状态 == true)
                {
                    if (lbl_料桶升降伺服状态.ForeColor != Color.Red)
                        lbl_料桶升降伺服状态.ForeColor = Color.Red;
                }
                else
                {
                    if (lbl_料桶升降伺服状态.ForeColor != Color.Blue)
                        lbl_料桶升降伺服状态.ForeColor = Color.Blue;
                }

                if (桶盖伺服状态 == true)
                {
                    if (lbl_桶盖伺服状态.ForeColor != Color.Red)
                        lbl_桶盖伺服状态.ForeColor = Color.Red;
                }
                else
                {
                    if (lbl_桶盖伺服状态.ForeColor != Color.Blue)
                        lbl_桶盖伺服状态.ForeColor = Color.Blue;
                }

                if (桶旋转伺服状态 == true)
                {
                    if (lbl_桶旋转伺服状态.ForeColor != Color.Red)
                        lbl_桶旋转伺服状态.ForeColor = Color.Red;
                }
                else
                {
                    if (lbl_桶旋转伺服状态.ForeColor != Color.Blue)
                        lbl_桶旋转伺服状态.ForeColor = Color.Blue;
                }


                if (桶翻转伺服状态 == true)
                {
                    if (lbl_桶翻转伺服状态.ForeColor != Color.Red)
                        lbl_桶翻转伺服状态.ForeColor = Color.Red;
                }
                else
                {
                    if (lbl_桶翻转伺服状态.ForeColor != Color.Blue)
                        lbl_桶翻转伺服状态.ForeColor = Color.Blue;
                }


                if (清理翻转伺服状态 == true)
                {
                    if (lbl_清理翻转伺服状态.ForeColor != Color.Red)
                        lbl_清理翻转伺服状态.ForeColor = Color.Red;
                }
                else
                {
                    if (lbl_清理翻转伺服状态.ForeColor != Color.Blue)
                        lbl_清理翻转伺服状态.ForeColor = Color.Blue;
                }


                if (清理升降伺服状态 == true)
                {
                    if (lbl_清理升降伺服状态.ForeColor != Color.Red)
                        lbl_清理升降伺服状态.ForeColor = Color.Red;
                }
                else
                {
                    if (lbl_清理升降伺服状态.ForeColor != Color.Blue)
                        lbl_清理升降伺服状态.ForeColor = Color.Blue;
                }

                if (下料摇筛伺服状态 == true)
                {
                    if (lbl_下料摇筛伺服状态.ForeColor != Color.Red)
                        lbl_下料摇筛伺服状态.ForeColor = Color.Red;
                }
                else
                {
                    if (lbl_下料摇筛伺服状态.ForeColor != Color.Blue)
                        lbl_下料摇筛伺服状态.ForeColor = Color.Blue;
                }

                #endregion

                #region 电机状态
                if (清扫电机1Q)
                {
                    btn_清扫电机启.BackColor = Color.Red;
                }
                else
                {
                    btn_清扫电机启.BackColor = Color.FromArgb(0, 0, 64);
                }


                if (电机50UQ)
                {
                    btn_50U电机启.BackColor = Color.Red;
                }
                else
                {
                    btn_50U电机启.BackColor = Color.FromArgb(0, 0, 64);
                }


                if (电磁阀11Q)
                {
                    btn_水阀11启.BackColor = Color.Red;
                }
                else
                {
                    btn_水阀11启.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (电磁阀12Q)
                {
                    btn_水阀12启.BackColor = Color.Red;
                }
                else
                {
                    btn_水阀12启.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (电磁阀13Q)
                {
                    btn_水阀13启.BackColor = Color.Red;
                }
                else
                {
                    btn_水阀13启.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (电磁阀21Q)
                {
                    btn_水阀21启.BackColor = Color.Red;
                }
                else
                {
                    btn_水阀21启.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (三轴缸1上Q)
                {
                    btn_三轴11上.BackColor = Color.Red;
                }
                else
                {
                    btn_三轴11上.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (三轴缸1下Q)
                {
                    btn_三轴11下.BackColor = Color.Red;
                }
                else
                {
                    btn_三轴11下.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (三轴缸3上Q)
                {
                    btn_三轴31上.BackColor = Color.Red;
                }
                else
                {
                    btn_三轴31上.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (三轴缸3下Q)
                {
                    btn_三轴31下.BackColor = Color.Red;
                }
                else
                {
                    btn_三轴31下.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (气振1Q)
                {
                    btn_气振11.BackColor = Color.Red;
                }
                else
                {
                    btn_气振11.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (气振2Q)
                {
                    btn_气振12.BackColor = Color.Red;
                }
                else
                {
                    btn_气振12.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (气振3Q)
                {
                    btn_气振13.BackColor = Color.Red;
                }
                else
                {
                    btn_气振13.BackColor = Color.FromArgb(0, 0, 64);
                }


                if (气振4Q)
                {
                    btn_气振14.BackColor = Color.Red;
                }
                else
                {
                    btn_气振14.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (气振5Q)
                {
                    btn_气振31.BackColor = Color.Red;
                }
                else
                {
                    btn_气振31.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (气振6Q)
                {
                    btn_气振32.BackColor = Color.Red;
                }
                else
                {
                    btn_气振32.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (气振7Q)
                {
                    btn_气振33.BackColor = Color.Red;
                }
                else
                {
                    btn_气振33.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (气振8Q)
                {
                    btn_气振34.BackColor = Color.Red;
                }
                else
                {
                    btn_气振34.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (无杆杠上Q)
                {
                    btn_无杆上.BackColor = Color.Red;
                }
                else
                {
                    btn_无杆上.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (无杆缸下Q)
                {
                    btn_无杆下.BackColor = Color.Red;
                }
                else
                {
                    btn_无杆下.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (电机振动1Q)
                {
                    btn_振动1启.BackColor = Color.Red;
                }
                else
                {
                    btn_振动1启.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (电机振动2Q)
                {
                    btn_振动2启.BackColor = Color.Red;
                }
                else
                {
                    btn_振动2启.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (电机振动3Q)
                {
                    btn_振动3启.BackColor = Color.Red;
                }
                else
                {
                    btn_振动3启.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (电机振动4Q)
                {
                    btn_振动4启.BackColor = Color.Red;
                }
                else
                {
                    btn_振动4启.BackColor = Color.FromArgb(0, 0, 64);
                }
                #endregion

                #region 液位
                txt_液位.Text = 液位.ToString();
                #endregion




            }));
        }

        private static byte[] ReadAsBytes(Plc plc, string address, int byteCount)
        {
            try
            {
                // 解析地址
                int startByte = int.Parse(address.Substring(2));

                // 读取字节数组
                var bytes = plc.ReadBytes(DataType.Memory, 0, startByte, (ushort)byteCount);

                // 处理字节顺序
                if (BitConverter.IsLittleEndian)
                {
                    bytes = bytes.Reverse().ToArray();
                }

                // 转换为Real
                float value = BitConverter.ToSingle(bytes, 0);
                Console.WriteLine($"{address} 字节方式读取Real值: {value}");
                return bytes;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"字节方式读取 {address} 失败: {ex.Message}");
            }
            return null;
        }

        private void buttonTimerStart_Click(object sender, EventArgs e)
        {
            btn_StartTimer.BackColor = Color.DodgerBlue;
            btn_StopTimer.BackColor = Color.FromArgb(0, 0, 64);
            // 启动计时器，每秒更新一次（1000毫秒）
            if (siemens!=null && siemens.IsConnected)
            {
                _timer.Change(0, 1000);

            }
            else { 
            
            }
        }

        private void buttonTimerStop_Click(object sender, EventArgs e)
        {
            btn_StartTimer.BackColor = Color.FromArgb(0, 0, 64);
            btn_StopTimer.BackColor = Color.DodgerBlue;
            // 停止计时器
            _timer.Change(Timeout.Infinite, Timeout.Infinite);
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            _timer?.Dispose();
        }

        private void Frm水洗装置_FormClosing(object sender, FormClosingEventArgs e)
        {
            //base.OnFormClosing(e);
            //_timer?.Dispose();
        }


        private void btnPlcConnect_Click(object sender, EventArgs e)
        {
            try
            {


                siemens = new Plc(CpuType.S71200, txtPlcIP.Text, 102, 0, 1);


                // 连接对象
                siemens.Open();
                if (!siemens.IsConnected)
                {
                    lblStatus.Text = "连接PLC失败！";
                }
                else
                {
                    lblStatus.Text = "连接PLC成功！";
                    readInitialValue();

                }
            }
            catch(Exception err)
            {
                MessageBox.Show("参数错误，请检查！"+ err.Message);
            }
        }

        private void readInitialValue() {

            //读取料桶升降伺服模式设定
            short 料桶升降伺服模式设定 = (short)siemens.Read(DataType.DataBlock, 4, 0, VarType.Int, 1);
            //Console.WriteLine($"读取值: {料桶升降伺服模式设定}");
            //读取桶盖伺服模式设定
            short 桶盖伺服模式设定 = (short)siemens.Read(DataType.DataBlock, 4, 34, VarType.Int, 1);
            //Console.WriteLine($"读取值: {桶盖伺服模式设定}");
            //读取桶旋转伺服模式设定
            short 桶旋转伺服模式设定 = (short)siemens.Read(DataType.DataBlock, 4, 68, VarType.Int, 1);
            //Console.WriteLine($"读取值: {桶旋转伺服模式设定}");
            //读取桶翻转伺服模式设定
            short 桶翻转伺服模式设定 = (short)siemens.Read(DataType.DataBlock, 4, 102, VarType.Int, 1);
            //Console.WriteLine($"读取值: {桶翻转伺服模式设定}");
            //读取清理翻转伺服模式设定
            short 清理翻转伺服模式设定 = (short)siemens.Read(DataType.DataBlock, 4, 136, VarType.Int, 1); ;
            //Console.WriteLine($"读取值: {清理翻转伺服模式设定}");
            //读取清理升降伺服模式设定
            short 清理升降伺服模式设定 = (short)siemens.Read(DataType.DataBlock, 4, 170, VarType.Int, 1);
            //Console.WriteLine($"读取值: {清理升降伺服模式设定}");
            //读取下料摇筛伺服模式设定
            short 下料摇筛伺服模式设定 = (short)siemens.Read(DataType.DataBlock, 4, 208, VarType.Int, 1);
            //Console.WriteLine($"读取值: {清理下料摇筛伺服模式设定}");
            if (txt_料桶升降伺服模式设定.Text != 料桶升降伺服模式设定.ToString())
                txt_料桶升降伺服模式设定.Text = 料桶升降伺服模式设定.ToString();

            if (txt_桶盖伺服模式设定.Text != 桶盖伺服模式设定.ToString())
                txt_桶盖伺服模式设定.Text = 桶盖伺服模式设定.ToString();

            if (txt_桶旋转伺服模式设定.Text != 桶旋转伺服模式设定.ToString())
                txt_桶旋转伺服模式设定.Text = 桶旋转伺服模式设定.ToString();

            if (txt_桶翻转伺服模式设定.Text != 桶翻转伺服模式设定.ToString())
                txt_桶翻转伺服模式设定.Text = 桶翻转伺服模式设定.ToString();

            if (txt_清理翻转伺服模式设定.Text != 清理翻转伺服模式设定.ToString())
                txt_清理翻转伺服模式设定.Text = 清理翻转伺服模式设定.ToString();

            if (txt_清理升降伺服模式设定.Text != 清理升降伺服模式设定.ToString())
                txt_清理升降伺服模式设定.Text = 清理升降伺服模式设定.ToString();

            if (txt_下料摇筛伺服模式设定.Text != 下料摇筛伺服模式设定.ToString())
                txt_下料摇筛伺服模式设定.Text = 下料摇筛伺服模式设定.ToString();

            //读取料桶升降伺服速度设定
            float 料桶升降伺服速度设定 = (float)siemens.Read(DataType.DataBlock, 4, 18, VarType.Real, 1);
            //Console.WriteLine($"读取值: {料桶升降伺服速度设定}");
            txt_料桶升降伺服速度设定.Text = 料桶升降伺服速度设定.ToString();

            //读取桶盖伺服速度设定
            float 桶盖伺服速度设定 = (float)siemens.Read(DataType.DataBlock, 4, 52, VarType.Real, 1);
            //Console.WriteLine($"读取值: {桶盖伺服速度设定}");
            txt_桶盖伺服速度设定.Text = 桶盖伺服速度设定.ToString();

            //读取桶旋转伺服速度设定
            float 桶旋转伺服速度设定 = (float)siemens.Read(DataType.DataBlock, 4, 86, VarType.Real, 1);
            //Console.WriteLine($"读取值: {桶旋转伺服速度设定}");
            txt_桶旋转伺服速度设定.Text = 桶旋转伺服速度设定.ToString();

            //读取桶翻转伺服速度设定
            float 桶翻转伺服速度设定 = (float)siemens.Read(DataType.DataBlock, 4, 120, VarType.Real, 1);
            //Console.WriteLine($"读取值: {桶翻转伺服速度设定}");
            txt_桶翻转伺服速度设定.Text = 桶翻转伺服速度设定.ToString();

            //读取清理翻转伺服速度设定
            float 清理翻转伺服速度设定 = (float)siemens.Read(DataType.DataBlock, 4, 154, VarType.Real, 1);
            //Console.WriteLine($"读取值: {清理翻转伺服速度设定}");
            txt_清理翻转伺服速度设定.Text = 清理翻转伺服速度设定.ToString();

            //读取清理升降伺服速度设定
            float 清理升降伺服速度设定 = (float)siemens.Read(DataType.DataBlock, 4, 188, VarType.Real, 1);
            //Console.WriteLine($"读取值: {清理升降伺服速度设定}");
            txt_清理升降伺服速度设定.Text = 清理升降伺服速度设定.ToString();

            //读取下料摇筛伺服速度设定
            float 下料摇筛伺服速度设定 = (float)siemens.Read(DataType.DataBlock, 4, 226, VarType.Real, 1);
            //Console.WriteLine($"读取值: {下料摇筛伺服速度设定}");
            txt_下料摇筛伺服速度设定.Text = 下料摇筛伺服速度设定.ToString();



            //读取料桶升降伺服目标位置
            float 料桶升降伺服目标位置 = (float)siemens.Read(DataType.DataBlock, 4, 6, VarType.Real, 1);
            //Console.WriteLine($"读取值: {料桶升降伺服目标位置}");
            txt_料桶升降伺服目标位置.Text = 料桶升降伺服目标位置.ToString();

            //读取桶盖伺服目标位置
            float 桶盖伺服目标位置 = (float)siemens.Read(DataType.DataBlock, 4, 44, VarType.Real, 1);
            //Console.WriteLine($"读取值: {桶盖伺服目标位置}");
            txt_桶盖伺服目标位置.Text = 桶盖伺服目标位置.ToString();

            //读取桶旋转伺服目标位置
            float 桶旋转伺服目标位置 = (float)siemens.Read(DataType.DataBlock, 4, 78, VarType.Real, 1);
            //Console.WriteLine($"读取值: {桶旋转伺服目标位置}");
            txt_桶旋转伺服目标位置.Text = 桶旋转伺服目标位置.ToString();

            //读取桶翻转伺服目标位置
            float 桶翻转伺服目标位置 = (float)siemens.Read(DataType.DataBlock, 4, 108, VarType.Real, 1);
            //Console.WriteLine($"读取值: {桶翻转伺服目标位置}");
            txt_桶翻转伺服目标位置.Text = 桶翻转伺服目标位置.ToString();

            //读取清理翻转伺服目标位置
            float 清理翻转伺服目标位置 = (float)siemens.Read(DataType.DataBlock, 4, 142, VarType.Real, 1);
            //Console.WriteLine($"读取值: {清理翻转伺服目标位置}");
            txt_清理翻转伺服目标位置.Text = 清理翻转伺服目标位置.ToString();

            //读取清理升降伺服目标位置
            float 清理升降伺服目标位置 = (float)siemens.Read(DataType.DataBlock, 4, 176, VarType.Real, 1);
            //Console.WriteLine($"读取值: {清理升降伺服目标位置}");
            txt_清理升降伺服目标位置.Text = 清理升降伺服目标位置.ToString();

            //读取下料摇筛伺服目标位置
            float 下料摇筛伺服目标位置 = (float)siemens.Read(DataType.DataBlock, 4, 214, VarType.Real, 1);
            //Console.WriteLine($"读取值: {下料摇筛伺服目标位置}");
            txt_下料摇筛伺服目标位置.Text = 下料摇筛伺服目标位置.ToString();



        }

        private void btnPlcDisconnect_Click(object sender, EventArgs e)
        {
            try
            {
                siemens.Close();
                lblStatus.Text = "PLC断开连接！";
                //updateTimer.Enabled = false;
            }
            catch
            {
                MessageBox.Show("参数错误，请检查！");
            }
        }

        private void btn_料桶升降伺服使能_Click(object sender, EventArgs e)
        {
            short valueRead = (short)siemens.Read(DataType.DataBlock, 4, 2, VarType.Int, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            if (siemens.Read("DB4.DBW2").ToString() == "1")
            {
                siemens.Write(DataType.DataBlock, 4,2, (short)0);
                //siemens.Write("DB4.DBW2", (short)0);
            }
            else
            {
                siemens.Write(DataType.DataBlock, 4, 2, (short)1);
                //siemens.Write("DB4.DBW2", (short)1);
            }
        }

        private void btn_料桶升降伺服模式设定_Click(object sender, EventArgs e)
        {
            short valueRead = (short)siemens.Read(DataType.DataBlock, 4, 0, VarType.Int, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            ushort newValue = ushort.Parse(txt_料桶升降伺服模式设定.Text);
            siemens.Write(DataType.DataBlock, 4, 0, newValue);

            //读取料桶模式设定
            short 料桶升降伺服模式设定 = (short)siemens.Read(DataType.DataBlock, 4, 0, VarType.Int, 1);
            //Console.WriteLine($"读取值: {料桶升降伺服模式设定}");
            txt_料桶升降伺服模式设定.Text = 料桶升降伺服模式设定.ToString();
        }

        private void btn_料桶升降伺服目标位置_Click(object sender, EventArgs e)
        {
            float valueRead = (float)siemens.Read(DataType.DataBlock, 4, 6, VarType.Real, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            float newValue = float.Parse(txt_料桶升降伺服目标位置.Text);

            siemens.Write(DataType.DataBlock, 4, 6, newValue);

            //读取料桶升降伺服目标设定
            float 料桶升降伺服目标设定 = (float)siemens.Read(DataType.DataBlock, 4, 6, VarType.Real, 1);
            //Console.WriteLine($"读取值: {料桶升降伺服目标设定}");
            txt_料桶升降伺服目标位置.Text = 料桶升降伺服目标设定.ToString();

        }


        private void btn_料桶升降伺服速度设定_Click(object sender, EventArgs e)
        {
            float valueRead = (float)siemens.Read(DataType.DataBlock, 4, 18, VarType.Real, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            float newValue = float.Parse(txt_料桶升降伺服速度设定.Text);

            siemens.Write(DataType.DataBlock, 4, 18, newValue);

            //读取料桶升降伺服速度设定
            float 料桶升降伺服速度设定 = (float)siemens.Read(DataType.DataBlock, 4, 18, VarType.Real, 1);
            //Console.WriteLine($"读取值: {料桶升降伺服速度设定}");
            txt_料桶升降伺服速度设定.Text = 料桶升降伺服速度设定.ToString();
        }



        private void btn_料桶升降伺服下降_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 4, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 4, 1, true);

        }

        private void btn_料桶升降伺服下降_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 4, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 4, 1, false);

        }



        private void btn_料桶升降伺服上升_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 4, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 4, 2, true);

        }

        private void btn_料桶升降伺服上升_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 4, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 4, 2, false);

        }

        private void btn_水阀11启_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 0, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 0, VarType.Bit, 1, 4))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 0, 4, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 0, 5, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }
        private void btn_水阀11停_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 0, VarType.Bit, 1, 5);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 0, VarType.Bit, 1, 5))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 0, 5, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 0, 4, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }


        private void btn_桶盖伺服模式设定_Click(object sender, EventArgs e)
        {
            short valueRead = (short)siemens.Read(DataType.DataBlock, 4, 34, VarType.Int, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            ushort newValue = ushort.Parse(txt_桶盖伺服模式设定.Text);
            siemens.Write(DataType.DataBlock, 4, 34, newValue);

            //读取桶盖伺服模式设定
            short 桶盖伺服模式设定 = (short)siemens.Read(DataType.DataBlock, 4, 34, VarType.Int, 1);
            //Console.WriteLine($"读取值: {桶盖伺服模式设定}");
            txt_桶盖伺服模式设定.Text = 桶盖伺服模式设定.ToString();
        }

        private void btn_桶盖伺服速度设定_Click(object sender, EventArgs e)
        {
            float valueRead = (float)siemens.Read(DataType.DataBlock, 4, 52, VarType.Real, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            float newValue = float.Parse(txt_桶盖伺服速度设定.Text);

            siemens.Write(DataType.DataBlock, 4, 52, newValue);

            //读取桶盖伺服速度设定
            float 桶盖伺服速度设定 = (float)siemens.Read(DataType.DataBlock, 4, 52, VarType.Real, 1);
            //Console.WriteLine($"读取值: {桶盖伺服速度设定}");
            txt_桶盖伺服速度设定.Text = 桶盖伺服速度设定.ToString();
        }

        private void btn_桶盖伺服目标位置_Click(object sender, EventArgs e)
        {
            float valueRead = (float)siemens.Read(DataType.DataBlock, 4, 44, VarType.Real, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            float newValue = float.Parse(txt_桶盖伺服目标位置.Text);

            siemens.Write(DataType.DataBlock, 4, 44, newValue);

            //读取桶盖伺服目标位置地址
            float 桶盖伺服目标位置 = (float)siemens.Read(DataType.DataBlock, 4, 44, VarType.Real, 1);
            //Console.WriteLine($"读取值: {桶盖伺服目标位置}");
            txt_桶盖伺服目标位置.Text = 桶盖伺服目标位置.ToString();
        }

        private void btn_桶盖伺服使能_Click(object sender, EventArgs e)
        {
            short valueRead = (short)siemens.Read(DataType.DataBlock, 4, 36, VarType.Int, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            if ((short)siemens.Read(DataType.DataBlock, 4, 36, VarType.Int, 1) == (short)1)
            {
                siemens.Write(DataType.DataBlock, 4, 36, (short)0);
                //siemens.Write("DB4.DBW2", (short)0);
            }
            else
            {
                siemens.Write(DataType.DataBlock, 4, 36, (short)1);
                //siemens.Write("DB4.DBW2", (short)1);
            }
        }


        private void btn_桶盖伺服开盖_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 38, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 38, 1, true);
        }

        private void btn_桶盖伺服开盖_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 38, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 38, 1, false);
        }

        private void btn_桶盖伺服合盖_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 38, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 38, 2, true);
        }

        private void btn_桶盖伺服合盖_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 38, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 38, 2, false);
        }

        private void btn_桶旋转伺服模式设定_Click(object sender, EventArgs e)
        {
            short valueRead = (short)siemens.Read(DataType.DataBlock, 4, 68, VarType.Int, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            ushort newValue = ushort.Parse(txt_桶旋转伺服模式设定.Text);
            siemens.Write(DataType.DataBlock, 4, 68, newValue);

            //读取桶旋转伺服模式设定
            short 桶旋转伺服模式设定 = (short)siemens.Read(DataType.DataBlock, 4, 68, VarType.Int, 1);
            //Console.WriteLine($"读取值: {桶旋转伺服模式设定}");
            txt_桶旋转伺服模式设定.Text = 桶旋转伺服模式设定.ToString();
        }

        private void btn_桶翻转伺服模式设定_Click(object sender, EventArgs e)
        {
            short valueRead = (short)siemens.Read(DataType.DataBlock, 4, 102, VarType.Int, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            ushort newValue = ushort.Parse(txt_桶翻转伺服模式设定.Text);
            siemens.Write(DataType.DataBlock, 4, 102, newValue);

            //读取桶翻转伺服模式设定
            short 桶翻转伺服模式设定 = (short)siemens.Read(DataType.DataBlock, 4, 102, VarType.Int, 1);
            //Console.WriteLine($"读取值: {桶翻转伺服模式设定}");
            txt_桶翻转伺服模式设定.Text = 桶翻转伺服模式设定.ToString();
        }

        private void btn_清理翻转伺服模式设定_Click(object sender, EventArgs e)
        {
            short valueRead = (short)siemens.Read(DataType.DataBlock, 4, 136, VarType.Int, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            ushort newValue = ushort.Parse(txt_清理翻转伺服模式设定.Text);
            siemens.Write(DataType.DataBlock, 4, 136, newValue);

            //读取清理翻转伺服模式设定
            short 清理翻转伺服模式设定 = (short)siemens.Read(DataType.DataBlock, 4, 136, VarType.Int, 1);
            //Console.WriteLine($"读取值: {清理翻转伺服模式设定}");
            txt_清理翻转伺服模式设定.Text = 清理翻转伺服模式设定.ToString();
        }

        private void btn_清理升降伺服模式设定_Click(object sender, EventArgs e)
        {
            short valueRead = (short)siemens.Read(DataType.DataBlock, 4, 170, VarType.Int, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            ushort newValue = ushort.Parse(txt_清理升降伺服模式设定.Text);
            siemens.Write(DataType.DataBlock, 4, 170, newValue);

            //读取清理升降伺服模式设定
            short 清理升降伺服模式设定 = (short)siemens.Read(DataType.DataBlock, 4, 170, VarType.Int, 1);
            //Console.WriteLine($"读取值: {清理升降伺服模式设定}");
            txt_清理升降伺服模式设定.Text = 清理升降伺服模式设定.ToString();
        }

        private void btn_桶旋转伺服速度设定_Click(object sender, EventArgs e)
        {
            float valueRead = (float)siemens.Read(DataType.DataBlock, 4, 86, VarType.Real, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            float newValue = float.Parse(txt_桶旋转伺服速度设定.Text);

            siemens.Write(DataType.DataBlock, 4, 86, newValue);

            //读取桶盖伺服速度设定
            float 桶旋转伺服速度设定 = (float)siemens.Read(DataType.DataBlock, 4, 86, VarType.Real, 1);
            //Console.WriteLine($"读取值: {桶旋转伺服速度设定}");
            txt_桶旋转伺服速度设定.Text = 桶旋转伺服速度设定.ToString();
        }

        private void btn_桶翻转伺服速度设定_Click(object sender, EventArgs e)
        {
            float valueRead = (float)siemens.Read(DataType.DataBlock, 4, 120, VarType.Real, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            float newValue = float.Parse(txt_桶翻转伺服速度设定.Text);

            siemens.Write(DataType.DataBlock, 4, 120, newValue);

            //读取桶翻转伺服速度设定
            float 桶翻转伺服速度设定 = (float)siemens.Read(DataType.DataBlock, 4, 120, VarType.Real, 1);
            //Console.WriteLine($"读取值: {桶翻转伺服速度设定}");
            txt_桶翻转伺服速度设定.Text = 桶翻转伺服速度设定.ToString();
        }

        private void btn_清理翻转伺服速度设定_Click(object sender, EventArgs e)
        {
            float valueRead = (float)siemens.Read(DataType.DataBlock, 4, 154, VarType.Real, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            float newValue = float.Parse(txt_清理翻转伺服速度设定.Text);

            siemens.Write(DataType.DataBlock, 4, 154, newValue);

            //读取清理翻转伺服速度设定
            float 清理翻转伺服速度设定 = (float)siemens.Read(DataType.DataBlock, 4, 154, VarType.Real, 1);
            //Console.WriteLine($"读取值: {清理翻转伺服速度设定}");
            txt_清理翻转伺服速度设定.Text = 清理翻转伺服速度设定.ToString();
        }

        private void btn_清理升降伺服速度设定_Click(object sender, EventArgs e)
        {
            float valueRead = (float)siemens.Read(DataType.DataBlock, 4, 226, VarType.Real, 0);
            //Console.WriteLine($"读取值: {valueRead}");

            float newValue = float.Parse(txt_清理升降伺服速度设定.Text);

            siemens.Write(DataType.DataBlock, 4, 226, newValue,0);

            //读取清理升降伺服速度设定
            float 清理升降伺服速度设定 = (float)siemens.Read(DataType.DataBlock, 4, 226, VarType.Real, 0);
            //Console.WriteLine($"读取值: {清理升降伺服速度设定}");
            txt_清理升降伺服速度设定.Text = 清理升降伺服速度设定.ToString();
        }

        private void btn_桶旋转伺服目标位置_Click(object sender, EventArgs e)
        {
            float valueRead = (float)siemens.Read(DataType.DataBlock, 4, 78, VarType.Real, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            float newValue = float.Parse(txt_桶旋转伺服目标位置.Text);

            siemens.Write(DataType.DataBlock, 4, 78, newValue);

            //读取桶旋转伺服目标位置
            float 桶旋转伺服目标位置 = (float)siemens.Read(DataType.DataBlock, 4, 78, VarType.Real, 1);
            //Console.WriteLine($"读取值: {桶旋转伺服目标位置}");
            txt_桶旋转伺服目标位置.Text = 桶旋转伺服目标位置.ToString();
        }

        private void btn_桶翻转伺服目标位置_Click(object sender, EventArgs e)
        {
            float valueRead = (float)siemens.Read(DataType.DataBlock, 4, 108, VarType.Real, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            float newValue = float.Parse(txt_桶翻转伺服目标位置.Text);

            siemens.Write(DataType.DataBlock, 4, 108, newValue);

            //读取桶翻转伺服目标位置
            float 桶翻转伺服目标位置 = (float)siemens.Read(DataType.DataBlock, 4, 108, VarType.Real, 1);
            //Console.WriteLine($"读取值: {桶翻转伺服目标位置}");
            txt_桶翻转伺服目标位置.Text = 桶翻转伺服目标位置.ToString();
        }

        private void btn_清理翻转伺服目标位置_Click(object sender, EventArgs e)
        {
            float valueRead = (float)siemens.Read(DataType.DataBlock, 4, 142, VarType.Real, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            float newValue = float.Parse(txt_清理翻转伺服目标位置.Text);

            siemens.Write(DataType.DataBlock, 4, 142, newValue);

            //读取清理翻转伺服目标位置
            float 清理翻转伺服目标位置 = (float)siemens.Read(DataType.DataBlock, 4, 142, VarType.Real, 1);
            //Console.WriteLine($"读取值: {清理翻转伺服目标位置}");
            txt_清理翻转伺服目标位置.Text = 清理翻转伺服目标位置.ToString();
        }

        private void btn_清理升降伺服目标位置_Click(object sender, EventArgs e)
        {
            float valueRead = (float)siemens.Read(DataType.DataBlock, 4, 176, VarType.Real, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            float newValue = float.Parse(txt_清理升降伺服目标位置.Text);

            siemens.Write(DataType.DataBlock, 4, 176, newValue);

            //读取清理升降伺服目标位置
            float 清理升降伺服目标位置 = (float)siemens.Read(DataType.DataBlock, 4, 176, VarType.Real, 1);
            //Console.WriteLine($"读取值: {清理升降伺服目标位置}");
            txt_清理升降伺服目标位置.Text = 清理升降伺服目标位置.ToString();
        }

        private void btn_桶旋转伺服使能_Click(object sender, EventArgs e)
        {
            short valueRead = (short)siemens.Read(DataType.DataBlock, 4, 70, VarType.Int, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            if ((short)siemens.Read(DataType.DataBlock, 4, 70, VarType.Int, 1) == (short)1)
            {
                siemens.Write(DataType.DataBlock, 4, 70, (short)0);
                //siemens.Write("DB4.DBW2", (short)0);
            }
            else
            {
                siemens.Write(DataType.DataBlock, 4, 70, (short)1);
                //siemens.Write("DB4.DBW2", (short)1);
            }
        }

        private void btn_桶翻转伺服使能_Click(object sender, EventArgs e)
        {
            short valueRead = (short)siemens.Read(DataType.DataBlock, 4, 104, VarType.Int, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            if ((short)siemens.Read(DataType.DataBlock, 4, 104, VarType.Int, 1) == (short)1)
            {
                siemens.Write(DataType.DataBlock, 4, 104, (short)0);
                //siemens.Write("DB4.DBW2", (short)0);
            }
            else
            {
                siemens.Write(DataType.DataBlock, 4, 104, (short)1);
                //siemens.Write("DB4.DBW2", (short)1);
            }
        }

        private void btn_清理翻转伺服使能_Click(object sender, EventArgs e)
        {
            short valueRead = (short)siemens.Read(DataType.DataBlock, 4, 138, VarType.Int, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            if ((short)siemens.Read(DataType.DataBlock, 4, 138, VarType.Int, 1) == (short)1)
            {
                siemens.Write(DataType.DataBlock, 4, 138, (short)0);
                //siemens.Write("DB4.DBW2", (short)0);
            }
            else
            {
                siemens.Write(DataType.DataBlock, 4, 138, (short)1);
                //siemens.Write("DB4.DBW2", (short)1);
            }
        }

        private void btn_清理升降伺服使能_Click(object sender, EventArgs e)
        {
            short valueRead = (short)siemens.Read(DataType.DataBlock, 4, 172, VarType.Int, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            if ((short)siemens.Read(DataType.DataBlock, 4, 172, VarType.Int, 1) == (short)1)
            {
                siemens.Write(DataType.DataBlock, 4, 172, (short)0);
                //siemens.Write("DB4.DBW2", (short)0);
            }
            else
            {
                siemens.Write(DataType.DataBlock, 4, 172, (short)1);
                //siemens.Write("DB4.DBW2", (short)1);
            }
        }

        private void btn_桶旋转伺服点动1_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 72, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 72, 1, true);
        }

        private void btn_桶旋转伺服点动1_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 72, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 72, 1, false);
        }

        private void btn_桶旋转伺服点动2_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 72, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 72, 2, true);
        }

        private void btn_桶旋转伺服点动2_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 72, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 72, 2, false);
        }

        private void btn_桶翻转伺服后仰_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 106, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 106, 1, true);
        }

        private void btn_桶翻转伺服后仰_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 106, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 106, 1, false);
        }

        private void btn_桶翻转伺服前倾_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 106, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 106, 2, true);
        }

        private void btn_桶翻转伺服前倾_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 106, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 106, 2, false);
        }

        private void btn_清理翻转伺服后仰_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 140, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 140, 1, true);
        }

        private void btn_清理翻转伺服后仰_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 140, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 140, 1, false);
        }

        private void btn_清理翻转伺服前倾_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 140, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 140, 2, true);
        }

        private void btn_清理翻转伺服前倾_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 140, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 140, 2, false);
        }

        private void btn_清理升降伺服点动1_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 174, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 174, 1, true);
        }

        private void btn_清理升降伺服点动1_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 174, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 174, 1, false);
        }

        private void btn_清理升降伺服点动2_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 174, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 174, 2, true);
        }

        private void btn_清理升降伺服点动2_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 174, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 174, 2, false);
        }

        private void btn_水阀12启_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 0, VarType.Bit, 1, 6);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 0, VarType.Bit, 1, 6))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 0, 6, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 0, 7, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}

        }



        private void btn_水阀12停_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 0, VarType.Bit, 1, 7);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 0, VarType.Bit, 1, 7))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 0, 7, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 0, 6, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_水阀13启_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 1, VarType.Bit, 1, 0);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 1, VarType.Bit, 1, 0))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 1, 0, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 1, 1, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_水阀13停_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 1, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 1, VarType.Bit, 1, 1))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 1, 1, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 1, 0, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_水阀21启_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 1, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 1, VarType.Bit, 1, 2))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 1, 2, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 1, 3, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_水阀21停_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 1, VarType.Bit, 1, 3);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 1, VarType.Bit, 1, 3))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 1, 3, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 1, 2, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_无杆上_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 4, VarType.Bit, 1, 0);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 4, VarType.Bit, 1, 0))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 4, 0, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 4, 0, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_三轴11上_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 1, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 1, VarType.Bit, 1, 4))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 1, 4, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 1, 4, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_三轴11下_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 1, VarType.Bit, 1, 5);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 1, VarType.Bit, 1, 5))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 1, 5, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 1, 5, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}

        }

        private void btn_三轴11停_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 1, VarType.Bit, 1, 6);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 1, VarType.Bit, 1, 6))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 1, 6, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 1, 6, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}

        }

        private void btn_三轴31上_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 2, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 2, VarType.Bit, 1, 2))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 2, 2, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 2, 2, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}

        }

        private void btn_三轴31下_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 2, VarType.Bit, 1, 3);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 2, VarType.Bit, 1, 3))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 2, 3, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 2, 3, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void 三轴31停_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 2, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 2, VarType.Bit, 1, 4))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 2, 4, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 2, 4, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_无杆下_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 4, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 4, VarType.Bit, 1, 1))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 4, 1, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 4, 1, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_无杆停_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 4, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 4, VarType.Bit, 1, 2))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 4, 2, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 4, 2, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_清扫电机启_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 0, VarType.Bit, 1, 0);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 0, VarType.Bit, 1, 0))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 0, 0, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 0, 0, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_清扫电机停_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 0, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 0, VarType.Bit, 1, 1))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 0, 1, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 0, 1, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_50U电机启_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 0, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 0, VarType.Bit, 1, 2))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 0, 2, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 0, 2, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_50U电机停_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 0, VarType.Bit, 1, 3);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 0, VarType.Bit, 1, 3))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 0, 3, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 0, 3, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_振动1启_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 4, VarType.Bit, 1, 3);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 4, VarType.Bit, 1, 3))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 4, 3, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 4, 3, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_振动1停_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 4, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 4, VarType.Bit, 1, 4))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 4, 4, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 4, 4, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_振动2启_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 4, VarType.Bit, 1, 5);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 4, VarType.Bit, 1, 5))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 4, 5, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 4, 5, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_振动2停_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 4, VarType.Bit, 1, 6);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 4, VarType.Bit, 1, 6))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 4, 6, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 4, 6, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_振动3启_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 4, VarType.Bit, 1, 7);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 4, VarType.Bit, 1, 7))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 4, 7, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 4, 7, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_振动3停_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 5, VarType.Bit, 1, 0);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 5, VarType.Bit, 1, 0))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 5, 0, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 5, 0, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_振动4启_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 5, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 5, VarType.Bit, 1, 1))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 5, 1, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 5, 1, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void btn_振动4停_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 5, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            //if ((bool)siemens.Read(DataType.DataBlock, 9, 5, VarType.Bit, 1, 2))
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 5, 2, true);
            //    //siemens.Write("DB4.DBW2", (short)0);
            //}
            //else
            //{
            siemens.WriteBit(DataType.DataBlock, 9, 5, 2, false);
            //    //siemens.Write("DB4.DBW2", (short)1);
            //}
        }

        private void button26_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 3, VarType.Bit, 1, 0);
            //Console.WriteLine($"读取值: {valueRead}");
            if ((bool)siemens.Read(DataType.DataBlock, 9, 3, VarType.Bit, 1, 0))
            {
                siemens.WriteBit(DataType.DataBlock, 9, 3, 0, false);
                //siemens.Write("DB4.DBW2", (short)0);
            }
            else
            {
                siemens.WriteBit(DataType.DataBlock, 9, 3, 0, true);
                //siemens.Write("DB4.DBW2", (short)1);
            }
        }

        private void button27_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 3, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            if ((bool)siemens.Read(DataType.DataBlock, 9, 3, VarType.Bit, 1, 1))
            {
                siemens.WriteBit(DataType.DataBlock, 9, 3, 1, false);
                //siemens.Write("DB4.DBW2", (short)0);
            }
            else
            {
                siemens.WriteBit(DataType.DataBlock, 9, 3, 1, true);
                //siemens.Write("DB4.DBW2", (short)1);
            }
        }

        private void button28_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 3, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            if ((bool)siemens.Read(DataType.DataBlock, 9, 3, VarType.Bit, 1, 2))
            {
                siemens.WriteBit(DataType.DataBlock, 9, 3, 2, false);
                //siemens.Write("DB4.DBW2", (short)0);
            }
            else
            {
                siemens.WriteBit(DataType.DataBlock, 9, 3, 2, true);
                //siemens.Write("DB4.DBW2", (short)1);
            }
        }

        private void button29_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 3, VarType.Bit, 1, 3);
            //Console.WriteLine($"读取值: {valueRead}");
            if ((bool)siemens.Read(DataType.DataBlock, 9, 3, VarType.Bit, 1, 3))
            {
                siemens.WriteBit(DataType.DataBlock, 9, 3, 3, false);
                //siemens.Write("DB4.DBW2", (short)0);
            }
            else
            {
                siemens.WriteBit(DataType.DataBlock, 9, 3, 3, true);
                //siemens.Write("DB4.DBW2", (short)1);
            }
        }

        private void button30_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 3, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            if ((bool)siemens.Read(DataType.DataBlock, 9, 3, VarType.Bit, 1, 4))
            {
                siemens.WriteBit(DataType.DataBlock, 9, 3, 4, false);
                //siemens.Write("DB4.DBW2", (short)0);
            }
            else
            {
                siemens.WriteBit(DataType.DataBlock, 9, 3, 4, true);
                //siemens.Write("DB4.DBW2", (short)1);
            }
        }

        private void button31_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 3, VarType.Bit, 1, 5);
            //Console.WriteLine($"读取值: {valueRead}");
            if ((bool)siemens.Read(DataType.DataBlock, 9, 3, VarType.Bit, 1, 5))
            {
                siemens.WriteBit(DataType.DataBlock, 9, 3, 5, false);
                //siemens.Write("DB4.DBW2", (short)0);
            }
            else
            {
                siemens.WriteBit(DataType.DataBlock, 9, 3, 5, true);
                //siemens.Write("DB4.DBW2", (short)1);
            }
        }

        private void button32_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 3, VarType.Bit, 1, 6);
            //Console.WriteLine($"读取值: {valueRead}");
            if ((bool)siemens.Read(DataType.DataBlock, 9, 3, VarType.Bit, 1, 6))
            {
                siemens.WriteBit(DataType.DataBlock, 9, 3, 6, false);
                //siemens.Write("DB4.DBW2", (short)0);
            }
            else
            {
                siemens.WriteBit(DataType.DataBlock, 9, 3, 6, true);
                //siemens.Write("DB4.DBW2", (short)1);
            }
        }

        private void button33_Click(object sender, EventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 9, 3, VarType.Bit, 1, 7);
            //Console.WriteLine($"读取值: {valueRead}");
            if ((bool)siemens.Read(DataType.DataBlock, 9, 3, VarType.Bit, 1, 7))
            {
                siemens.WriteBit(DataType.DataBlock, 9, 3, 7, false);
                //siemens.Write("DB4.DBW2", (short)0);
            }
            else
            {
                siemens.WriteBit(DataType.DataBlock, 9, 3, 7, true);
                //siemens.Write("DB4.DBW2", (short)1);
            }
        }


        public async void ClickAfterSeconds(Button button, int seconds)
        {

            // 延时指定秒数
            // 执行点击
            Console.WriteLine("调用"+ button.ToString() + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            button.PerformClick();
        }

        private async void btn_一键启动1_Click(object sender, EventArgs e)
        {

            //初始化状态 
            //伺服驱动器使能
            if ((short)siemens.Read(DataType.DataBlock, 4, 2, VarType.Int, 1) == 0)
                btn_料桶升降伺服使能.PerformClick();
            if ((short)siemens.Read(DataType.DataBlock, 4, 36, VarType.Int, 1)==0)
                btn_桶盖伺服使能.PerformClick();
            if ((short)siemens.Read(DataType.DataBlock, 4, 70, VarType.Int, 1) == 0)
                btn_桶旋转伺服使能.PerformClick();
            if ((short)siemens.Read(DataType.DataBlock, 4, 104, VarType.Int, 1) == 0)
                btn_桶翻转伺服使能.PerformClick();

            //桶开盖
            await Task.Delay(1 * 1000);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            txt_桶盖伺服目标位置.Text = "-70";
            btn_桶盖伺服目标位置.PerformClick();
            await Task.Delay(1 * 1000);
            btn_桶盖伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_桶盖伺服启动_MouseUp(sender, (MouseEventArgs)e);


            //桶上升
            //await Task.Delay(1 * 1000);
            await ProcessWithConditionAsync("桶盖伺服", -70);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            tb_任务状态信息.Text = "桶上升_" + System.DateTime.Now.ToString("HH:mm:ss.fff");
            txt_料桶升降伺服目标位置.Text = "75";
            btn_料桶升降伺服目标位置.PerformClick();
            await Task.Delay(1 * 1000);
            btn_料桶升降伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_料桶升降伺服启动_MouseUp(sender, (MouseEventArgs)e);

            //桶注水

            await ProcessWithConditionAsync("料桶升降伺服", 75);
            //await Task.Delay(20 * 1000);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            btn_水阀11启.PerformClick();
            await Task.Delay(5 * 1000);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            btn_水阀11停.PerformClick();

            //桶下降
            //await Task.Delay(1 * 1000);
            await ProcessWithConditionAsync("料桶升降伺服", 75);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            tb_任务状态信息.Text = "桶上升_" + System.DateTime.Now.ToString("HH:mm:ss.fff");
            txt_料桶升降伺服目标位置.Text = "65";
            btn_料桶升降伺服目标位置.PerformClick();
            await Task.Delay(1 * 1000);
            btn_料桶升降伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_料桶升降伺服启动_MouseUp(sender, (MouseEventArgs)e);


            //桶合盖
            await ProcessWithConditionAsync("料桶升降伺服", 65);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            txt_桶盖伺服目标位置.Text = "175";
            btn_桶盖伺服目标位置.PerformClick() ;
            await Task.Delay(1 * 1000);
            btn_桶盖伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_桶盖伺服启动_MouseUp(sender, (MouseEventArgs)e);

            //桶上升
            //await Task.Delay(1 * 1000);
            await ProcessWithConditionAsync("桶盖伺服", 175);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            tb_任务状态信息.Text = "桶上升_" + System.DateTime.Now.ToString("HH:mm:ss.fff");
            txt_料桶升降伺服目标位置.Text = "75";
            btn_料桶升降伺服目标位置.PerformClick();
            await Task.Delay(1 * 1000);
            btn_料桶升降伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_料桶升降伺服启动_MouseUp(sender, (MouseEventArgs)e);



            //桶睡倒
            //await Task.Delay(5 * 1000);
            await ProcessWithConditionAsync("料桶升降伺服", 75);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            txt_桶翻转伺服目标位置.Text = "90";
            btn_桶翻转伺服目标位置.PerformClick();
            await Task.Delay(1 * 1000);
            btn_桶翻转伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_桶翻转伺服启动_MouseUp(sender, (MouseEventArgs)e);

            //桶旋转
            //await Task.Delay(10 * 1000);
            await ProcessWithConditionAsync("桶翻转伺服", 90);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            txt_桶旋转伺服目标位置.Text = "1800";
            btn_桶旋转伺服目标位置.PerformClick();
            await Task.Delay(1 * 1000);
            btn_桶旋转伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_桶旋转伺服启动_MouseUp(sender, (MouseEventArgs)e);

            //桶倒转
            //await Task.Delay(10 * 1000);
            await ProcessWithConditionAsync("桶旋转伺服", 1800);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            txt_桶旋转伺服目标位置.Text = "0";
            btn_桶旋转伺服目标位置.PerformClick();
            await Task.Delay(1 * 1000);
            btn_桶旋转伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_桶旋转伺服启动_MouseUp(sender, (MouseEventArgs)e);

            //桶立起
            //await Task.Delay(20 * 1000);
            await ProcessWithConditionAsync("桶旋转伺服", 0);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            txt_桶翻转伺服目标位置.Text = "40";
            btn_桶翻转伺服目标位置.PerformClick();
            await Task.Delay(1 * 1000);
            btn_桶翻转伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_桶翻转伺服启动_MouseUp(sender, (MouseEventArgs)e);


            //桶开盖
            //await Task.Delay(10 * 1000);
            await ProcessWithConditionAsync("桶翻转伺服", 40);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            txt_桶盖伺服目标位置.Text = "80";
            btn_桶盖伺服目标位置.PerformClick();
            await Task.Delay(1 * 1000);
            btn_桶盖伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_桶盖伺服启动_MouseUp(sender, (MouseEventArgs)e);

            ////气杆压紧筛子
            //await Task.Delay(1 * 1000);
            ////Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            //btn_无杆上.PerformClick();
            //await Task.Delay(5 * 1000);
            ////Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            //btn_无杆停.PerformClick();


            ////接料振动
            //await Task.Delay(1 * 1000);
            ////Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            //btn_振动1启.PerformClick();
            //await Task.Delay(5 * 1000);
            ////Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            //btn_振动1停.PerformClick();

            ////水幕开
            ////await Task.Delay(5 * 1000);
            //await ProcessWithConditionAsync("桶盖伺服", 0);
            ////Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            //btn_水阀12启.PerformClick();
            //await Task.Delay(5 * 1000);
            ////Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            //btn_水阀12停.PerformClick();

            //桶倒水
            await ProcessWithConditionAsync("桶盖伺服", 80);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            txt_桶翻转伺服目标位置.Text = "102";
            btn_桶翻转伺服目标位置.PerformClick();
            await Task.Delay(1 * 1000);
            btn_桶翻转伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_桶翻转伺服启动_MouseUp(sender, (MouseEventArgs)e);

            ////桶斜喷水
            //await ProcessWithConditionAsync("桶翻转伺服", 135);
            //await Task.Delay(3 * 1000);
            ////Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            //btn_水阀11启.PerformClick();
            //await Task.Delay(5 * 1000);
            ////Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            //btn_水阀11停.PerformClick();

            //桶旋转
            await ProcessWithConditionAsync("桶翻转伺服", 102);
            await Task.Delay(3 * 1000);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            txt_桶旋转伺服目标位置.Text = "1800";
            btn_桶旋转伺服目标位置.PerformClick();
            await Task.Delay(1 * 1000);
            btn_桶旋转伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_桶旋转伺服启动_MouseUp(sender, (MouseEventArgs)e);

            //桶倒转
            //await Task.Delay(10 * 1000);
            await ProcessWithConditionAsync("桶旋转伺服", 1800);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            txt_桶旋转伺服目标位置.Text = "0";
            btn_桶旋转伺服目标位置.PerformClick();
            await Task.Delay(1 * 1000);
            btn_桶旋转伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_桶旋转伺服启动_MouseUp(sender, (MouseEventArgs)e);

            //桶开盖
            //await Task.Delay(10 * 1000);
            await ProcessWithConditionAsync("桶旋转伺服", 0);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            txt_桶盖伺服目标位置.Text = "-70";
            btn_桶盖伺服目标位置.PerformClick();
            await Task.Delay(1 * 1000);
            btn_桶盖伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_桶盖伺服启动_MouseUp(sender, (MouseEventArgs)e);


            //桶立起
            //await Task.Delay(15 * 1000);
            await ProcessWithConditionAsync("桶盖伺服", -70);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            txt_桶翻转伺服目标位置.Text = "0";
            btn_桶翻转伺服目标位置.PerformClick();
            await Task.Delay(1 * 1000);
            btn_桶翻转伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_桶翻转伺服启动_MouseUp(sender, (MouseEventArgs)e);

            //桶下降回位
            await ProcessWithConditionAsync("桶翻转伺服", 0);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            tb_任务状态信息.Text = "桶下降_" + System.DateTime.Now.ToString("HH:mm:ss.fff");
            txt_料桶升降伺服目标位置.Text = "40";
            btn_料桶升降伺服目标位置.PerformClick();
            await Task.Delay(1 * 1000);
            btn_料桶升降伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_料桶升降伺服启动_MouseUp(sender, (MouseEventArgs)e);

            //桶合盖
            await ProcessWithConditionAsync("料桶升降伺服", 40);
            //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            txt_桶盖伺服目标位置.Text = "175";
            btn_桶盖伺服目标位置.PerformClick();
            await Task.Delay(1 * 1000);
            btn_桶盖伺服启动_MouseDown(sender, (MouseEventArgs)e);
            btn_桶盖伺服启动_MouseUp(sender, (MouseEventArgs)e);



            ////筛上喷水前出
            //await ProcessWithConditionAsync("桶翻转伺服", 0);
            ////Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            //btn_三轴11上.PerformClick();
            //await Task.Delay(5 * 1000);
            ////Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            //btn_三轴11停.PerformClick();

            ////喷水电磁阀打开
            //await Task.Delay(2 * 1000);
            ////Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            //btn_水阀13启.PerformClick();


            ////喷水电磁阀关闭
            //await Task.Delay(10 * 1000);
            ////Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            //btn_水阀13停.PerformClick();

            ////气杆松开筛子
            //await Task.Delay(2 * 1000);
            ////Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            //btn_无杆下.PerformClick();
            //await Task.Delay(10 * 1000);
            ////Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            //btn_无杆停.PerformClick();


            //关闭伺服使能
            await Task.Delay(2 * 1000);
            await ProcessWithConditionAsync("桶盖伺服", 175);

            if ((short)siemens.Read(DataType.DataBlock, 4, 2, VarType.Int, 1) == 1)
                btn_料桶升降伺服使能.PerformClick();
            if ((short)siemens.Read(DataType.DataBlock, 4, 36, VarType.Int, 1) == 1)
                btn_桶盖伺服使能.PerformClick();
            if ((short)siemens.Read(DataType.DataBlock, 4, 70, VarType.Int, 1) == 1)
                btn_桶旋转伺服使能.PerformClick();
            if ((short)siemens.Read(DataType.DataBlock, 4, 104, VarType.Int, 1) == 1)
                btn_桶翻转伺服使能.PerformClick();

        }

        async Task ProcessWithConditionAsync(string source,float  var)
        {
            while (true)
            {
                // 异步等待一段时间（避免阻塞）
                await Task.Delay(100); // 每100毫秒检查一次

                // 检查条件
                if (CheckCondition(source ,var))
                {
                    break; // 条件满足，退出循环
                }
            }

            // 条件满足后继续执行的代码
            Console.WriteLine("条件已满足，继续执行...");
        }

        bool CheckCondition(string source,float var)
        {
            switch (source)
            {
                case "料桶升降伺服":
                    // 返回你的业务逻辑条件
                    float 料桶升降伺服实际位置 = (float)siemens.Read(DataType.DataBlock, 4, 24, VarType.Real, 1);
                    return 料桶升降伺服实际位置 == var; // 示例：当秒数≥30时继续

                case "桶盖伺服":
                    // 返回你的业务逻辑条件
                    float 桶盖伺服实际位置 = (float)siemens.Read(DataType.DataBlock, 4, 62, VarType.Real, 1);
                    return 桶盖伺服实际位置 == var; // 示例：当秒数≥30时继续

                case "桶旋转伺服":
                    // 返回你的业务逻辑条件
                    float 桶旋转伺服实际位置 = (float)siemens.Read(DataType.DataBlock, 4, 96, VarType.Real, 1);
                    return 桶旋转伺服实际位置 == var; // 示例：当秒数≥30时继续

                case "桶翻转伺服":
                    // 返回你的业务逻辑条件
                    float 桶翻转伺服实际位置 = (float)siemens.Read(DataType.DataBlock, 4, 126, VarType.Real, 1);
                    return 桶翻转伺服实际位置 == var; // 示例：当秒数≥30时继续

                case "清理翻转伺服":
                    // 返回你的业务逻辑条件
                    float 清理翻转伺服实际位置 = (float)siemens.Read(DataType.DataBlock, 4, 160, VarType.Real, 1);
                    return 清理翻转伺服实际位置 == var; // 示例：当秒数≥30时继续

                case "清理升降伺服":
                    // 返回你的业务逻辑条件
                    float 清理升降伺服实际位置 = (float)siemens.Read(DataType.DataBlock, 4, 194, VarType.Real, 1);
                    return 清理升降伺服实际位置 == var; // 示例：当秒数≥30时继续

                default:
                    break;
            }
            return false;
        }


        private async void btn_一键启动2_Click(object sender, EventArgs e)
        {
            //初始化状态 
            //伺服驱动器使能
            if ((short)siemens.Read(DataType.DataBlock, 4, 138, VarType.Int, 1) == 0)
                btn_清理翻转伺服使能.PerformClick();
            if ((short)siemens.Read(DataType.DataBlock, 4, 172, VarType.Int, 1) == 0)
                btn_清理升降伺服使能.PerformClick();

            btn_一键启动2.BackColor = Color.SpringGreen;
            do
            {
                //气杆压紧筛子
                await Task.Delay(2 * 1000);
                //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
                btn_三轴31上.PerformClick();
                await Task.Delay(3 * 1000);
                //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
                btn_三轴31停.PerformClick();

                //振动打开
                await Task.Delay(3 * 1000);
                //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
                btn_振动3启.PerformClick();

                //振动停止
                await Task.Delay(2 * 1000);
                //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
                btn_振动3停.PerformClick();

                //翻转倒料
                await Task.Delay(4 * 1000);
                //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
                txt_清理翻转伺服速度设定.Text = "40";
                btn_清理翻转伺服速度设定.PerformClick();
                txt_清理翻转伺服目标位置.Text = "180";
                btn_清理翻转伺服目标位置.PerformClick();
                await Task.Delay(1 * 1000);
                btn_清理翻转伺服启动_MouseDown(sender, (MouseEventArgs)e);
                btn_清理翻转伺服启动_MouseUp(sender, (MouseEventArgs)e);


                //翻转回来
                await ProcessWithConditionAsync("清理翻转伺服", 180);
                //await Task.Delay(12 * 1000);
                //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
                txt_清理翻转伺服目标位置.Text = "0";
                btn_清理翻转伺服目标位置.PerformClick();
                await Task.Delay(1 * 1000);
                btn_清理翻转伺服启动_MouseDown(sender, (MouseEventArgs)e);
                btn_清理翻转伺服启动_MouseUp(sender, (MouseEventArgs)e);

                //电刷旋转
                //await Task.Delay(10 * 1000);
                await ProcessWithConditionAsync("清理翻转伺服", 0);
                //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
                btn_清扫电机启.PerformClick();


                //电刷降低
                await Task.Delay(1 * 1000);
                //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
                tb_任务状态信息.Text = "电刷下降_" + System.DateTime.Now.ToString("HH:mm:ss.fff");
                txt_清理升降伺服目标位置.Text = "17";
                btn_清理升降伺服目标位置.PerformClick();
                await Task.Delay(1 * 1000);
                btn_清理升降伺服启动_MouseDown(sender, (MouseEventArgs)e);
                btn_清理升降伺服启动_MouseUp(sender, (MouseEventArgs)e);

                //电刷升高
                await ProcessWithConditionAsync("清理升降伺服", 17);
                await Task.Delay(3 * 1000);
                //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
                tb_任务状态信息.Text = "电刷上升_" + System.DateTime.Now.ToString("HH:mm:ss.fff");
                txt_清理升降伺服目标位置.Text = "0";
                btn_清理升降伺服目标位置.PerformClick();
                await Task.Delay(1 * 1000);
                btn_清理升降伺服启动_MouseDown(sender, (MouseEventArgs)e);
                btn_清理升降伺服启动_MouseUp(sender, (MouseEventArgs)e);


                //电刷停转
                //await Task.Delay(10 * 1000);
                await ProcessWithConditionAsync("清理升降伺服", 0);
                //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
                btn_清扫电机停.PerformClick();


                //翻转倒料
                await Task.Delay(1 * 1000);
                //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
                txt_清理翻转伺服目标位置.Text = "180";
                btn_清理翻转伺服目标位置.PerformClick();
                await Task.Delay(1 * 1000);
                btn_清理翻转伺服启动_MouseDown(sender, (MouseEventArgs)e);
                btn_清理翻转伺服启动_MouseUp(sender, (MouseEventArgs)e);



                //翻转回来
                //await Task.Delay(20 * 1000);
                await ProcessWithConditionAsync("清理翻转伺服", 180);
                //Console.WriteLine("调用-" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
                txt_清理翻转伺服目标位置.Text = "0";
                btn_清理翻转伺服目标位置.PerformClick();
                await Task.Delay(1 * 1000);
                btn_清理翻转伺服启动_MouseDown(sender, (MouseEventArgs)e);
                btn_清理翻转伺服启动_MouseUp(sender, (MouseEventArgs)e);


            } while (ckb_一键2循环.Checked);
                //关闭伺服使能
                //await Task.Delay(20 * 1000);
                await ProcessWithConditionAsync("清理翻转伺服", 0);
            if ((short)siemens.Read(DataType.DataBlock, 4, 138, VarType.Int, 1) == 1)
                btn_清理翻转伺服使能.PerformClick();
            if ((short)siemens.Read(DataType.DataBlock, 4, 172, VarType.Int, 1) == 1)
                btn_清理升降伺服使能.PerformClick();
                await Task.Delay(2 * 1000);
                btn_一键启动2.BackColor = Color.LightSeaGreen;
        }

        private void btn_料桶升降伺服启动_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 4, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 4, 4, true);
        }

        private void btn_料桶升降伺服启动_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 4, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 4, 4, false);
        }

        private void btn_桶盖伺服启动_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 38, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 38, 4, true);
        }

        private void btn_桶盖伺服启动_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 38, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 38, 4, false);
        }

        private void btn_桶旋转伺服启动_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 72, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 72, 4, true);
        }

        private void btn_桶旋转伺服启动_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 72, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 72, 4, false);
        }

        private void btn_桶翻转伺服启动_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 106, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 106, 4, true);
        }

        private void btn_桶翻转伺服启动_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 106, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 106, 4, false);
        }

        private void btn_清理翻转伺服启动_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 140, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 140, 4, true);
        }

        private void btn_清理翻转伺服启动_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 140, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 140, 4, false);
        }

        private void btn_清理升降伺服启动_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 174, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 174, 4, true);
        }

        private void btn_清理升降伺服启动_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 174, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 174, 4, false);
        }

        private void tb_Click(object sender, EventArgs e)
        {
            TextBox tb_temp = (TextBox)sender;
            FrmSoftKey.input = tb_temp.Text;
            // 获取 TextBox 的屏幕位置
            Point textboxLocation = tb_temp.PointToScreen(Point.Empty);
            // 创建一个新的窗体
            FrmSoftKey frmSoftKey = new FrmSoftKey();
            // 计算窗体的位置
            int popupX = textboxLocation.X;
            int popupY = textboxLocation.Y + tb_temp.Size.Height;
            // 设置窗体的位置
            frmSoftKey.Location = new Point(popupX, popupY);
            // 显示窗体
            frmSoftKey.ShowDialog();
            //MessageBox.Show("显示键盘之后");
            //MessageBox.Show(FrmSoftKey.input);
            if (FrmSoftKey.confirm)
            {
                tb_temp.Text = FrmSoftKey.input;
                tb_temp.SelectionStart = tb_temp.Text.Length;
                tb_temp.SelectionLength = 0;
                tb_temp.Focus();
            }
        }

        private void btn_下料摇筛伺服模式设定_Click(object sender, EventArgs e)
        {
            short valueRead = (short)siemens.Read(DataType.DataBlock, 4, 208, VarType.Int, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            ushort newValue = ushort.Parse(txt_下料摇筛伺服模式设定.Text);
            siemens.Write(DataType.DataBlock, 4, 208, newValue);

            //读取下料摇筛伺服模式设定
            short 下料摇筛伺服模式设定 = (short)siemens.Read(DataType.DataBlock, 4, 208, VarType.Int, 1);
            //Console.WriteLine($"读取值: {下料摇筛伺服模式设定}");
            txt_下料摇筛伺服模式设定.Text = 下料摇筛伺服模式设定.ToString();
        }

        private void btn_下料摇筛伺服速度设定_Click(object sender, EventArgs e)
        {
            float valueRead = (float)siemens.Read(DataType.DataBlock, 4, 226, VarType.Real, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            float newValue = float.Parse(txt_下料摇筛伺服速度设定.Text);

            siemens.Write(DataType.DataBlock, 4, 226, newValue);

            //读取下料摇筛伺服速度设定
            float 下料摇筛伺服速度设定 = (float)siemens.Read(DataType.DataBlock, 4, 226, VarType.Real, 1);
            //Console.WriteLine($"读取值: {下料摇筛伺服速度设定}");
            txt_下料摇筛伺服速度设定.Text = 下料摇筛伺服速度设定.ToString();
        }

        private void btn_下料摇筛伺服目标位置_Click(object sender, EventArgs e)
        {
            float valueRead = (float)siemens.Read(DataType.DataBlock, 4, 214, VarType.Real, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            float newValue = float.Parse(txt_下料摇筛伺服目标位置.Text);

            siemens.Write(DataType.DataBlock, 4, 214, newValue);

            //读取下料摇筛伺服目标位置
            float 下料摇筛伺服目标位置 = (float)siemens.Read(DataType.DataBlock, 4, 214, VarType.Real, 1);
            //Console.WriteLine($"读取值: {下料摇筛伺服目标位置}");
            txt_下料摇筛伺服目标位置.Text = 下料摇筛伺服目标位置.ToString();
        }

        private void btn_下料摇筛伺服使能_Click(object sender, EventArgs e)
        {
            short valueRead = (short)siemens.Read(DataType.DataBlock, 4, 210, VarType.Int, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            if ((short)siemens.Read(DataType.DataBlock, 4, 210, VarType.Int, 1) == (short)1)
            {
                siemens.Write(DataType.DataBlock, 4, 210, (short)0);
                //siemens.Write("DB4.DBW2", (short)0);
            }
            else
            {
                siemens.Write(DataType.DataBlock, 4, 210, (short)1);
                //siemens.Write("DB4.DBW2", (short)1);
            }
        }

        private void btn_下料摇筛伺服启动_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 212, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 212, 4, true);
        }

        private void btn_下料摇筛伺服启动_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 212, VarType.Bit, 1, 4);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 212, 4, false);
        }

        private void btn_下料摇筛伺服点动1_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 212, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 212, 1, true);
        }

        private void btn_下料摇筛伺服点动1_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 212, VarType.Bit, 1, 1);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 212, 1, false);
        }

        private void btn_下料摇筛伺服点动2_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 212, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 212, 2, true);
        }

        private void btn_下料摇筛伺服点动2_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 4, 212, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 4, 212, 2, false);
        }
    }
}
